<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/user' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/user/info' }">个人信息</el-breadcrumb-item>
        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div>
      <el-card>
        <el-form :model="changePasswordFormInfo" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="员工姓名">
                <el-input v-model="changePasswordFormInfo.empName" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="员工编号">
                <el-input v-model="changePasswordFormInfo.empId" disabled></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-form ref="password" :rules="rules" :model="changePasswordForm" label-width="80px">
          <el-row :gutter="20">
            <el-col :span="10">
              <el-form-item label="密码" prop="password">
                <el-input v-model="changePasswordForm.password" type="password" placeholder="请输入密码" clearable
                          show-password></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="确认密码" prop="password2">
                <el-input v-model="changePasswordForm.password2" type="password" placeholder="请输入确认密码" clearable
                          show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="btn">
          <el-button type="primary" @click="changePassword">提交</el-button>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "ChangePassword",
  data() {
    const checkPassword = (rule, value, callback) => {
      if (this.changePasswordForm.password !== value) return callback(new Error('2次密码输入不一致'))
      // 否则校验成功
      callback()
    }
    return {
      changePasswordFormInfo: {},
      changePasswordForm: {},
      rules: {
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur'}
        ],
        password2: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur'},
          {validator: checkPassword, trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    changePassword() {
      const emp = {}
      emp.empId = this.$cookies.get("empId")
      emp.empName = this.$cookies.get("username")
      emp.empPassword = this.changePasswordForm.password
      this.$refs.password.validate(async valid => {
        if (!valid) return this.$message.error("表单校验失败,密码有误")
        const {data: res} = await this.$http.put('/user/changeInfo/password', emp).catch(function (e) {
          alert(e)
        })
        console.log(res)
        if (res.status !== 200) return this.$message.error("修改密码失败")
        await this.$router.push('/')
        this.$message.success('修改密码成功,请重新登录')
      })
    }
  },
  created() {
    this.changePasswordFormInfo.empId = this.$cookies.get("empId")
    this.changePasswordFormInfo.empName = this.$cookies.get("username")
  }
}
</script>

<style lang="less" scoped>
.el-form .el-form-item {
  margin-top: 20px;
  margin-left: 150px;
}

.el-button {
  margin: 20px auto;
}

.btn {
  text-align: center;
}
</style>
